package prop;

import org.zjx.prop.db.ItemInstanceDB;
import org.zjx.prop.db.ItemSlotDB;
import org.zjx.prop.db.ItemTemplateDB;
import org.zjx.prop.domain.enums.ItemType;
import org.zjx.prop.domain.enums.SlotStatus;
import org.zjx.prop.domain.enums.SlotType;
import org.zjx.prop.domain.model.ItemInstance;
import org.zjx.prop.domain.model.ItemSlot;
import org.zjx.prop.domain.model.ItemTemplate;
import org.zjx.prop.repository.ItemInstanceRepository;
import org.zjx.prop.repository.ItemSlotRepository;
import org.zjx.prop.repository.ItemTemplateRepository;
import org.zjx.prop.repository.impl.ItemInstanceRepositoryImpl;
import org.zjx.prop.repository.impl.ItemSlotRepositoryImpl;
import org.zjx.prop.repository.impl.ItemTemplateRepositoryImpl;
import org.zjx.prop.service.ItemManager;
import org.zjx.prop.service.ItemStackService;
import org.zjx.prop.service.ItemValidator;
import org.zjx.prop.service.impl.ItemManagerImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DatabaseInitializer {

    public static void main(String[] args) {
        DatabaseInitializer.initialize();
    }

    public static void initialize() {

        // 创建表
        ItemTemplateDB templateDB = new ItemTemplateDB();
        ItemSlotDB slotDB = new ItemSlotDB();
        ItemInstanceDB instanceDB = new ItemInstanceDB();

        // 清空现有数据
        templateDB.clearData();
        slotDB.clearData();
        instanceDB.clearData();

        // 初始化道具模板数据
        initItemTemplates(templateDB);

        // 初始化槽位数据
        initItemSlots(slotDB);

        // 初始化玩家道具数据
        initPlayerItems(instanceDB);


        List<ItemTemplate> itemTemplate = new ItemTemplateDB().findAll();
        System.out.println(itemTemplate);

        ItemInstanceRepository instanceRepo = new ItemInstanceRepositoryImpl(instanceDB);
        ItemSlotRepository slotRepo = new ItemSlotRepositoryImpl(slotDB);
        ItemTemplateRepository templateRepo = new ItemTemplateRepositoryImpl(templateDB);

        ItemStackService stackService = new ItemStackService(templateRepo);
        ItemValidator validator = new ItemValidator(slotRepo, templateRepo);

        ItemManager itemManager = new ItemManagerImpl(instanceRepo, slotRepo, templateRepo, stackService, validator);



    }

    private static void initItemTemplates(ItemTemplateDB templateDB) {
        // 药品
        ItemTemplate potion = new ItemTemplate();
        potion.setId(1001);
        potion.setName("金创药");
        potion.setDescription("恢复少量气血");
        potion.setType(ItemType.MEDICINE);
        potion.setMaxStack(30);
        Map<String, Object> potionAttrs = new HashMap<>();
        potionAttrs.put("hp_recover", 200);
        potion.setAttributes(potionAttrs);
        templateDB.insert(potion);

        // 武器
        ItemTemplate weapon = new ItemTemplate();
        weapon.setId(2001);
        weapon.setName("游龙剑");
        weapon.setDescription("60级剑");
        weapon.setType(ItemType.WEAPON);
        weapon.setLevel(60);
        Map<String, Object> weaponAttrs = new HashMap<>();
        weaponAttrs.put("damage", 320);
        weaponAttrs.put("hit", 45);
        weaponAttrs.put("durability", 500);
        weapon.setAttributes(weaponAttrs);
        templateDB.insert(weapon);

        // 灵饰
        ItemTemplate ring = new ItemTemplate();
        ring.setId(3001);
        ring.setName("明月珰");
        ring.setDescription("60级戒指");
        ring.setType(ItemType.SPECIAL_EQUIP);
        ring.setLevel(60);
        Map<String, Object> ringAttrs = new HashMap<>();
        ringAttrs.put("damage", 25);
        ringAttrs.put("critical", 3);
        ring.setAttributes(ringAttrs);
        templateDB.insert(ring);
    }

    private static void initItemSlots(ItemSlotDB slotDB) {
        // 装备栏位
        for (int i = 0; i < 6; i++) {
            ItemSlot slot = new ItemSlot();
            slot.setId(i + 1);
            slot.setSlotType(SlotType.EQUIPMENT);
            slot.setPosition(i);
            slot.setStatus(SlotStatus.NORMAL);
            slotDB.insert(slot);
        }

        // 灵饰栏位
        for (int i = 0; i < 4; i++) {
            ItemSlot slot = new ItemSlot();
            slot.setId(i + 7);
            slot.setSlotType(SlotType.SPECIAL_EQUIP);
            slot.setPosition(i);
            slot.setStatus(SlotStatus.NORMAL);
            slotDB.insert(slot);
        }

        // 道具栏(20个)
        for (int i = 0; i < 20; i++) {
            ItemSlot slot = new ItemSlot();
            slot.setId(i + 11);
            slot.setSlotType(SlotType.INVENTORY);
            slot.setPosition(i);
            slot.setStatus(SlotStatus.NORMAL);
            slotDB.insert(slot);
        }
    }

    private static void initPlayerItems(ItemInstanceDB instanceDB) {
        // 玩家1001的道具
        ItemInstance weapon = new ItemInstance();
        weapon.setPlayerId("1001");
        weapon.setTemplateId(2001); // 游龙剑
        weapon.setSlotId(1); // 武器槽位
        weapon.setDurability(450);
        weapon.setDurabilityEmptyDelete(true);
        Map<String, Object> weaponAttrs = new HashMap<>();
        weaponAttrs.put("enhance_level", 6);
        weapon.setAttributes(weaponAttrs);
        instanceDB.insert(weapon);

        ItemInstance potion = new ItemInstance();
        potion.setPlayerId("1001");
        potion.setTemplateId(1001); // 金创药
        potion.setSlotId(11); // 第一个道具栏
        potion.setDurability(15);
        potion.setDurabilityEmptyDelete(true);
        instanceDB.insert(potion);
    }
}